

#include <stdint.h>

#define STRIDE_FACTOR           (415)
#define RUNNING_FACTOR          (830)
#define CALORIE_FACTOR          (1036)
#define CALORIE_DEVIDER         (1000000)
#define DISTANCE_DEVIDER        (100000)

/*
    m = mile / 0.00062137
*/
#define METER2MILE              62137
#define METER2MILE_NORMALIZE    10000000

typedef struct 
{
    uint8_t gender;
    uint8_t age;
    uint8_t height;
    uint8_t weight;   

    uint32_t stride;
    uint32_t calorie;
}algo_config_t;

static algo_config_t m_algo_config = {.age = 23, .gender = 0, .height = 170, .weight = 75 };

void algo_lib_pedo_set_profile(uint8_t gender, uint8_t age, uint8_t height, uint8_t weight)
{
    m_algo_config.gender = gender;
    m_algo_config.age = age;
    m_algo_config.height = height;
    m_algo_config.weight = weight;

    m_algo_config.stride = height * STRIDE_FACTOR;
    m_algo_config.calorie = weight * CALORIE_FACTOR;
}

void algo_lib_pedo_update_profile_weight(uint8_t weight)
{
    m_algo_config.weight = weight;
    m_algo_config.calorie = weight * CALORIE_FACTOR;
}
void algo_lib_pedo_update_profile_height(uint8_t height)
{
    m_algo_config.height = height;
    m_algo_config.stride = height * STRIDE_FACTOR; 
}


uint32_t algo_lib_pedo_get_distance_unit_meter(uint32_t steps)
{
    return steps * m_algo_config.stride / DISTANCE_DEVIDER;
}

uint32_t algo_lib_pedo_get_distance_unit_kilo_meter(uint32_t steps)
{
    return algo_lib_pedo_get_distance_unit_meter(steps)/10;
}

uint32_t algo_lib_pedo_get_distance_unit_mile(uint32_t steps )
{
    return algo_lib_pedo_get_distance_unit_meter(steps) * METER2MILE /  METER2MILE_NORMALIZE;
}

 uint32_t algo_lib_pedo_get_calorie(uint32_t steps )
{
    return algo_lib_pedo_get_distance_unit_meter(steps) * m_algo_config.calorie / CALORIE_DEVIDER;
}

uint32_t algo_lib_pedo_get_calorie_with_distance(uint32_t distance)
{
    return distance * m_algo_config.calorie / CALORIE_DEVIDER;
}

uint32_t algo_lib_pedo_get_steps_of_kilo(void)
{
    return 1000 * DISTANCE_DEVIDER / m_algo_config.stride;
}
